<?php

namespace frontend\controllers\bazi;

use Yii;
use yii\web\BadRequestHttpException;

class AutobaziController extends \yii\web\Controller
{
    public function actionIndex()
    {
        // 1970年1月1号是己酉年丙子月辛巳日戊子时
        $start = strtotime("1970-01-01 00:00:00");
        $year = rand(1970, 2020);
        $month = rand(3,12);
        $day = rand(1, 31);
        if (!checkdate($month, $day, $year)) {
            $day = rand(1, 28);
        }
        $hour = rand(1, 23);
        $minute = rand(1, 60);
        $second = rand(1, 60);
        $end = strtotime("$year-$month-$day $hour:$minute:$second");
        if ($end < 0) {
            $hour = rand(8,23);
            $end = strtotime("$year-$month-$day $hour:$minute:$second");
        }
//        $year = 1983;
//        $month = 10;
//        $day = 13;
        $jiazi_year = ($year - 1970) % 60 + 46 + 1;
        if ($jiazi_year > 60) {
            $jiazi_year = $jiazi_year % 60;
        }
        $jiazi_month = (($year - 1970) * 12 + $month - 1) % 60 + 13 + 1;
        if ($jiazi_month > 60) {
            $jiazi_month = $jiazi_month % 60;
        }
        $jiazi_day = intval($end / 86400)  % 60 + 18;
        if ($jiazi_day > 60) {
            $jiazi_day = $jiazi_day % 60;
        }
//        echo $jiazi_day;exit;
        $jiazi_hour = intval($end / 86400) % 5 + 3;
        if ($jiazi_hour > 5) {
            $jiazi_hour = $jiazi_hour % 5;
        }
        $arr_jiazi_shi = array_chunk(Yii::$app->params['jiazi'],12,false);
        $dizhi_shi = $this->mbStrSplit($arr_jiazi_shi[$jiazi_hour-1][rand(0, 11)]);
        $date = $year."年".$month."月".$day."日<br />";

        $str_jiazi_year = $this->mbStrSplit(Yii::$app->params['jiazi'][$jiazi_year]);
        $str_jiazi_month = $this->mbStrSplit(Yii::$app->params['jiazi'][$jiazi_month]);
        $str_jiazi_day = $this->mbStrSplit(Yii::$app->params['jiazi'][$jiazi_day]);

        $arr_tiangan[] = $dizhi_shi[0];
        $arr_tiangan[] = $str_jiazi_day[0];
        $arr_tiangan[] = $str_jiazi_month[0];
        $arr_tiangan[] = $str_jiazi_year[0];

        $arr_dizhi[] = $dizhi_shi[1];
        $arr_dizhi[] = $str_jiazi_day[1];
        $arr_dizhi[] = $str_jiazi_month[1];
        $arr_dizhi[] = $str_jiazi_year[1];

        $type = Yii::$app->request->get('type');

        return $this->render(
            'index',
            array(
                'date' => $date,
                'tiangan' => $arr_tiangan,
                'dizhi' => $arr_dizhi,
                'tiangan_color' => Yii::$app->params['tiangan_color'],
                'dizhi_color' => Yii::$app->params['dizhi_color'],
                'type' => $type
            )
        );
    }

    public function mbStrSplit ($string, $len=1) {
        $start = 0;
        $strlen = mb_strlen($string);
        while ($strlen) {
            $array[] = mb_substr($string,$start,$len,"utf8");
            $string = mb_substr($string, $len, $strlen,"utf8");
            $strlen = mb_strlen($string);
        }
        return $array;
    }

}